package com.tz.tzgw.pc.web;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.util.SaResult;
import com.tz.tzgw.pc.web.annotation.AdminControllerAnnotation;
import com.tz.tzgw.pc.web.dao.*;
import com.tz.tzgw.pc.web.model.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author tqq
 * 专题后台控制器
 */
@Controller
@AdminControllerAnnotation
@RequestMapping("/api/")
public class ZhuantiController {
    private final ZtArticleDao ztArticleDao;

    private final ZtVideoDao ztVideoDao;

    private final ZtImagesDao ztImagesDao;

    private final ZtVideoCateDao ztVideoCateDao;


    private final ZtSetDao ztSetDao;
    public ZhuantiController(ZtArticleDao ztArticleDao,ZtVideoDao ztVideoDao,ZtImagesDao ztImagesDao,ZtVideoCateDao ztVideoCateDao,ZtSetDao ztSetDao) {

        this.ztArticleDao = ztArticleDao;
        this.ztVideoDao = ztVideoDao;
        this.ztImagesDao = ztImagesDao;
        this.ztVideoCateDao = ztVideoCateDao;
        this.ztSetDao = ztSetDao;
    }

    @RequestMapping("get_zt_video_list")
    @ResponseBody
    @SaCheckLogin
    public SaResult get_zt_video_list(@RequestParam("page") Integer pageNum, @RequestParam(value = "setid", defaultValue = "") Long setid,@RequestParam(value = "cateid", defaultValue = "") Integer cateid) {
        PageResult<ZtVideo> pageResult = ztVideoDao.getZtVideosByPageAndCondition(pageNum,20,setid,null,cateid);
        if (pageResult != null) {
            return SaResult.data(pageResult);
        } else {
            // 数据不存在，返回错误信息
            return SaResult.error("数据不存在");
        }
    }

    @RequestMapping("zt_video_act_do")
    @ResponseBody
    @SaCheckLogin
    public SaResult zt_video_act_do(@RequestBody ZtVideo ztVideo) {
        int suc = ztVideoDao.insertOrUpdateZtVideo(ztVideo);
        if (suc  == 1) {
            return SaResult.data(ztVideo);
        } else {
            return SaResult.error("更新失败");
        }
    }

    @RequestMapping("zt_video_del")
    @ResponseBody
    @SaCheckLogin
    public SaResult zt_video_del(@RequestParam("id") Integer id) {
        int suc = ztVideoDao.deleteVideo(id);
        if (suc  == 1) {
            return SaResult.data(1);
        } else {
            return SaResult.error("更新失败");
        }
    }


    @RequestMapping("get_zt_article_list")
    @ResponseBody
    @SaCheckLogin
    public SaResult get_zt_article_list(@RequestParam("page") Integer pageNum, @RequestParam(value = "title", defaultValue = "") String title,@RequestParam(value = "setid", defaultValue = "") Long setid) {
        PageResult<ZtArticle> pageResult = ztArticleDao.getZtArticlesByPageAndCondition(pageNum,20,setid);
        if (pageResult != null) {
            return SaResult.data(pageResult);
        } else {
            // 数据不存在，返回错误信息
            return SaResult.error("数据不存在");
        }
    }

    @RequestMapping("zt_article_act_do")
    @ResponseBody
    @SaCheckLogin
    public SaResult zt_article_act_do(@RequestBody ZtArticle ztArticle) {
        int suc = ztArticleDao.insertOrUpdateZtArticle(ztArticle);
        if (suc  == 1) {
            return SaResult.data(ztArticle);
        } else {
            return SaResult.error("更新失败");
        }
    }

    @RequestMapping("get_zt_article_detail")
    @ResponseBody
    @SaCheckLogin
    public SaResult get_zt_article_detail(@RequestParam("id") Long id) {
        ZtArticle article = ztArticleDao.getArticleById(id);
        if (article != null) {
            return SaResult.data(article);
        } else {
            // 数据不存在，返回错误信息
            return SaResult.error("数据不存在");
        }
    }


    @RequestMapping("zt_article_del")
    @ResponseBody
    @SaCheckLogin
    public SaResult zt_article_del(@RequestParam("id") Integer id) {
        int suc = ztArticleDao.deleteArticle(id);
        if (suc  == 1) {
            return SaResult.data(1);
        } else {
            return SaResult.error("更新失败");
        }
    }


//    @RequestMapping("get_zt_images")
//    @ResponseBody
//    @SaCheckLogin
//    public SaResult get_zt_images(@RequestParam("columnid") Long columnid) {
//        ZtImages images = ztImagesDao.getImagesByColumnid(columnid);
//        if (images != null) {
//            return SaResult.data(images);
//        } else {
//            // 数据不存在，返回错误信息
//            return SaResult.error("数据不存在");
//        }
//    }
//
//
//    @RequestMapping("zt_images_act_do")
//    @ResponseBody
//    @SaCheckLogin
//    public SaResult zt_images_act_do(@RequestBody ZtImages ztImages) {
//        int suc = ztImagesDao.insertOrUpdateZtImages(ztImages);
//        if (suc  == 1) {
//            return SaResult.data(ztImages);
//        } else {
//            return SaResult.error("更新失败");
//        }
//    }




    @RequestMapping("get_zt_set_all_list")
    @ResponseBody
    @SaCheckLogin
    public SaResult get_zt_set_all_list() {
        List<ZtSet> list = ztSetDao.getAllZtSets(null);
        if (list != null) {
            return SaResult.data(list);
        } else {
            // 数据不存在，返回错误信息
            return SaResult.error("数据不存在");
        }
    }





    @RequestMapping("get_zt_images_list")
    @ResponseBody
    @SaCheckLogin
    public SaResult get_zt_images_list(@RequestParam("page") Integer pageNum, @RequestParam(value = "status", defaultValue = "") Integer status,@RequestParam(value = "setid", defaultValue = "") Long setid) {
        PageResult<ZtImages> pageResult = ztImagesDao.getZtImagesByPageAndCondition(pageNum,20,setid,status);
        if (pageResult != null) {
            return SaResult.data(pageResult);
        } else {
            // 数据不存在，返回错误信息
            return SaResult.error("数据不存在");
        }
    }

    @RequestMapping("zt_images_act_do")
    @ResponseBody
    @SaCheckLogin
    public SaResult zt_images_act_do(@RequestBody ZtImages ztImages) {
        int suc = ztImagesDao.insertOrUpdateZtImages(ztImages);
        if (suc  == 1) {
            return SaResult.data(ztImages);
        } else {
            return SaResult.error("更新失败");
        }
    }

    @RequestMapping("get_zt_images_detail")
    @ResponseBody
    @SaCheckLogin
    public SaResult get_zt_images_detail(@RequestParam("id") Long id) {
        ZtImages images = ztImagesDao.getImagesById(id);
        if (images != null) {
            return SaResult.data(images);
        } else {
            // 数据不存在，返回错误信息
            return SaResult.error("数据不存在");
        }
    }


    @RequestMapping("zt_images_del")
    @ResponseBody
    @SaCheckLogin
    public SaResult zt_images_del(@RequestParam("id") Integer id) {
        int suc = ztImagesDao.deleteImages(id);
        if (suc  == 1) {
            return SaResult.data(1);
        } else {
            return SaResult.error("更新失败");
        }
    }





    @RequestMapping("get_zt_video_cate_list")
    @ResponseBody
    @SaCheckLogin
    public SaResult get_zt_video_cate_list(@RequestParam(value = "setid", defaultValue = "") Long setid) {
        List<ZtVideoCate> cateresult = ztVideoCateDao.getList(setid);
        if (cateresult != null) {
            return SaResult.data(cateresult);
        } else {
            // 数据不存在，返回错误信息
            return SaResult.error("数据不存在");
        }
    }

    @RequestMapping("zt_video_cate_act_do")
    @ResponseBody
    @SaCheckLogin
    public SaResult zt_video_cate_act_do(@RequestBody ZtVideoCate ztVideoCate) {
        int suc = ztVideoCateDao.insertOrUpdateCate(ztVideoCate);
        if (suc  == 1) {
            return SaResult.data(ztVideoCate);
        } else {
            return SaResult.error("更新失败");
        }
    }

    @RequestMapping("zt_video_cate_del")
    @ResponseBody
    @SaCheckLogin
    public SaResult zt_video_cate_del(@RequestParam("id") Integer id) {
        int suc = ztVideoCateDao.deleteCate(id);
        if (suc  == 1) {
            return SaResult.data(1);
        } else {
            return SaResult.error("更新失败");
        }
    }


//    --------------------专题设置---------------


    /**
     * 后台专题列表接口
     * @param pageNum
     * @param pageSize
     * @param name
     * @return
     */
    @RequestMapping("get_zt_set_list")
    @ResponseBody
    @SaCheckLogin
    public SaResult get_zt_set_list(@RequestParam("page") Integer pageNum, @RequestParam(value = "page_size", defaultValue = "") Integer pageSize,@RequestParam(value = "name", defaultValue = "") String name) {
        PageResult<ZtSet> pageResult = ztSetDao.getZtSetByPageAndCondition(pageNum,pageSize,name,null);
        if (pageResult != null) {
            return SaResult.data(pageResult);
        } else {
            // 数据不存在，返回错误信息
            return SaResult.error("数据不存在");
        }
    }

    /**
     * 后台专题设置增改
     * @param ztSet
     * @return
     */
    @RequestMapping("zt_set_act_do")
    @ResponseBody
    @SaCheckLogin
    public SaResult zt_set_act_do(@RequestBody ZtSet ztSet) {
        int suc = ztSetDao.insertOrUpdateZtSet(ztSet);
        if (suc  == 1) {
            return SaResult.data(ztSet);
        } else {
            return SaResult.error("更新失败");
        }
    }

    /**
     * 后台专题设置删除
     * @param id
     * @return
     */
    @RequestMapping("zt_set_del")
    @ResponseBody
    @SaCheckLogin
    public SaResult zt_set_del(@RequestParam("id") Integer id) {
        int suc = ztSetDao.deleteZtSet(id);
        if (suc  == 1) {
            return SaResult.data(1);
        } else {
            return SaResult.error("更新失败");
        }
    }
















}
